﻿using Lincolnwood.Foundation.SermonLibrary.Data.NHibernate;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using NHibernate;
using Lincolnwood.Foundation.SermonLibrary.Data;
using Lincolnwood.Foundation.SermonLibrary;
using System.Collections.Generic;
using Lincolnwood.Foundation.Common.Data.NHibernate;
using Lincolnwood.Foundation.Common.Data;

namespace Lincolnwood.Foundation.SermonLibrary.Tests
{
    /// <summary>
    ///This is a test class for NHibernateSermonRepositoryTest and is intended
    ///to contain all NHibernateSermonRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class NHibernateSermonRepositoryTest
    {
        private TestContext testContextInstance;        
        private NHibernateHelper _nHibernateHelper = new NHibernateHelper();
        private IUnitOfWork _unitOfWork;

        public NHibernateSermonRepositoryTest()
        {
            _nHibernateHelper.ConfigureWith(typeof(NHibernateSermonRepository).Assembly);
        }

        [TestInitialize]
        public void TestInitialize()
        {
            _nHibernateHelper.GenerateSchema();
            _unitOfWork = new NHibernateSermonLibraryUnitOfWork();
        }

        [TestMethod]
        public void Can_Generate_Schema()
        {
            _nHibernateHelper.GenerateSchema();
        }

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Lincolnwood.Foundation.SermonLibrary.Data.ISermonRepository.FindById
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Lincolnwood.Foundation.SermonLibrary.dll")]
        public void FindByIdTest()
        {

            ISermonRepository testRepository = new NHibernateSermonRepository(_unitOfWork);
            Sermon testSermon = new Sermon(
                "Test Sermon Title", 
                "Test sermon description", 
                "Conrad",
                "Murrell", 
                "Pastor", 
                new DateTime(2012, 1, 13), 
                "Lincolnwood Baptist Church",
                new MediaLink("Sermonss123.mp3", MediaType.Audio));

            testRepository.Add(testSermon);

            var testSermonId = testSermon.Id;

            _unitOfWork.Flush();


            Sermon actual;
            actual = testRepository.FindById(testSermonId);

            Assert.AreEqual(testSermon, actual);
        }

        /// <summary>
        ///A test for Lincolnwood.Foundation.SermonLibrary.Data.ISermonRepository.Add
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Lincolnwood.Foundation.SermonLibrary.dll")]
        public void AddTest()
        {

            ISermonRepository testRepository = new NHibernateSermonRepository(_unitOfWork);
            Sermon testSermon = new Sermon(
                "Test Sermon Title",
                "Test sermon description",
                "Conrad",
                "Murrell",
                "Pastor",
                new DateTime(2012, 1, 13),
                "Lincolnwood Baptist Church",
                new MediaLink("Sermonss123.mp3", MediaType.Audio));

            testRepository.Add(testSermon);

            var testSermonId = testSermon.Id;

            _unitOfWork.Flush();

        }

        /// <summary>
        ///A test for GetSermonsPaged
        ///</summary>
        [TestMethod()]
        public void GetSermonsPagedTest()
        {

            ISermonRepository testRepository = new NHibernateSermonRepository(_unitOfWork);

            Sermon testSermon1 = new Sermon(
                "Test Sermon Title1",
                "Test sermon description1",
                "Conrad",
                "Murrell",
                "Pastor",
                new DateTime(2012, 1, 13),
                "Lincolnwood Baptist Church",
                new MediaLink("Sermonss123.mp3", MediaType.Audio));

            Sermon testSermon2 = new Sermon(
                "Test Sermon Title2",
                "Test sermon description2",
                "Conrad",
                "Murrell",
                "Pastor",
                new DateTime(2012, 1, 13),
                "Lincolnwood Baptist Church",
                new MediaLink("Sermonss123.mp3", MediaType.Audio));

            testRepository.Add(testSermon1);
            testRepository.Add(testSermon2);

            _unitOfWork.Flush();


            var pagedActual = testRepository.GetSermonsPaged(1, 2, PublishedStatus.Any);
        }


        /// <summary>
        ///A test for Lincolnwood.Foundation.SermonLibrary.Data.ISermonRepository.RemoveById
        ///</summary>
        [TestMethod()]
        [DeploymentItem("Lincolnwood.Foundation.SermonLibrary.dll")]
        public void RemoveByIdTest()
        {

            ISermonRepository testRepository = new NHibernateSermonRepository(_unitOfWork);
            Sermon testSermon = new Sermon(
                "Test Sermon Title",
                "Test sermon description",
                "Conrad",
                "Murrell",
                "Pastor",
                new DateTime(2012, 1, 13),
                "Lincolnwood Baptist Church",
                new MediaLink("Sermonss123.mp3", MediaType.Audio));

            testRepository.Add(testSermon);

            var testSermonId = testSermon.Id;

            _unitOfWork.Flush();


            Sermon actual;
            actual = testRepository.FindById(testSermonId);

            Assert.AreEqual(testSermon, actual);


            testRepository.RemoveById(testSermonId);

            var shouldBeNull = testRepository.FindById(testSermonId);

            Assert.IsNull(shouldBeNull);
        }

        [TestMethod()]
        public void FindBySermonSeriesIdTest()
        {

            ISermonSeriesRepository seriesRepository = new NHibernateSermonSeriesRepository(_unitOfWork);
            SermonSeries testSeries = new SermonSeries("Test Series 1", "Test Series 1 Description");
            seriesRepository.Add(testSeries);

            ISermonRepository testRepository = new NHibernateSermonRepository(_unitOfWork);
            Sermon testSermon = new Sermon(
                "Test Sermon Title",
                "Test sermon description",
                "Conrad",
                "Murrell",
                "Pastor",
                new DateTime(2012, 1, 13),
                "Lincolnwood Baptist Church",
                new MediaLink("Sermonss123.mp3", MediaType.Audio));
            
            testSermon.Series = testSeries;

            testRepository.Add(testSermon);

            var testSermonId = testSermon.Id;

            _unitOfWork.Flush();


            var result = testRepository.GetSermonsInSeries(testSeries.Id);

            Assert.AreEqual(1, result.Count());
            Assert.AreEqual(testSermon.Title, result.ElementAt(0).Title);

        }
    }
}
